package ModelLayer;
import java.util.ArrayList;


public class LoanRegister
{
    private ArrayList<Loan> loan;
    private static LoanRegister instance;
    
    private LoanRegister()
    {
        loan = new ArrayList<Loan>();
    }

    public static LoanRegister getInstance()
    {
        if( null == instance )
        {
            instance = new LoanRegister();
            return instance;
        }
        return instance;
    }
    
    public void setLoan(int id, String borrowDate, int period,Copy copy, Person person)
    {
        Loan loaN= new Loan(id, borrowDate, period, copy, person);
        loan.add(loaN);
        person.addLoan(loaN);
        copy.setLoan(loaN);

    }
    
    public ArrayList<Loan> getLoan()
    {
        return loan;
    }
    
    public void deleteLoan(int id)
    {
        int index = loan.indexOf( findLoan(id) ) ;
        loan.remove(index);
    }
    
    public Loan findLoan(int id)
    {
        int index = 0;
        for(;index < loan.size(); index++)
        {
            if( loan.get(index).getId()==id )
            {
                return loan.get(index);
            }
        }
        return null;
    }
    
    public void editLoan(Loan loan, String borrowDate, int period)
    {
      loan.setDate(borrowDate);
      loan.setPeriod(period);
    }
    
    public void deleteLoan(Loan loaN)
    {
        loan.remove(loaN);
    }
    
    public String printLoans()
    {
        String s="";
        int index=0;
        while(index<loan.size())
        {            
            s=loan.get(index).toString()+'\n';
            index++;
        }
        return s;
    }
}
